<?php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Game\Server;
use App\Models\Game\ServerGroup;
use App\Models\Menu;
use App\Services\AdminCacheService;
use App\Services\RedisService;
use App\Utils\ResponseUtil;
use Illuminate\Support\Facades\Auth;

class IndexController extends Controller
{
    private AdminCacheService $adminCacheService;
    public function __construct(AdminCacheService $adminCacheService)
    {
        $this->adminCacheService = $adminCacheService;
    }

    public function init(RedisService $redisService){ //系统首页初始化数据
       /* $initData = $redisService->get('initData',function(){
            $data = [];
            $data['homeInfo'] = [
                'title'=>'首页',
                'href'=>'page/welcome-1.html?t=1',
            ];
            $data['logoInfo'] = [
                'title'=>'项目二',
                'href'=>'',
                'image'=>'images/logo.png'
            ];
            $data['web_url'] = config('app.web_url');
            $services = Server::query()->select('server_id','name','can_push')->get()->toArray();
            $data['services'] = $services;
            return json_encode($data);
        },24*3600);

        $data = json_decode($initData,true);*/
        $data = [];
        $data['homeInfo'] = [
            'title'=>'首页',
            'href'=>'page/welcome-1.html?t=1',
        ];
        $data['logoInfo'] = [
            'title'=>'晨曦战纪',
            'href'=>'',
            'image'=>'images/logo.png'
        ];
        $data['web_url'] = config('app.web_url');

        //菜单具有权限功能,不做缓存

        $admin = Auth::guard('admin')->user();
        $permissionIds = $admin->permissions->pluck('id')->toArray();
        $groups_ids = $admin['groups_ids'];
        $groups_ids_array = explode(",", $groups_ids);
        if ($groups_ids) {
            $services = Server::query()
                ->whereIn('group_id', $groups_ids_array)
                ->select('server_id', 'name', 'can_push')
                ->orderByRaw('CAST(server_id AS UNSIGNED) ASC')
                ->get()
                ->toArray();
        } else {
            $services = Server::query()
                ->select('server_id', 'name', 'can_push')
                ->orderByRaw('CAST(server_id AS UNSIGNED) ASC')
                ->get()
                ->toArray();
        }
        $data['services'] = $services;
        $MenuModel = Menu::query();
        $MenuModel->where('status',1); //显示状态
        $MenuModel->where('is_menu',1); //菜单:true
        if ($admin->id!=1){
            $MenuModel->whereIn('id',$permissionIds); //权限内
        }
        
        $menus = $MenuModel->orderBy('sort','asc')->get();
        $data['menuInfo'] = self::buildMenu($menus);
        return ResponseUtil::json($data);
    }

    private function buildMenu($menus,$pid = 0){ //构造递归数组
        $newMenu = array_values(collect($menus)->where('pid',$pid)->all());
        foreach($newMenu as &$item){
            $item['child'] = self::buildMenu($menus,data_get($item,'id'));
        }
        return $newMenu;
    }

    public function clearCache(){
        $this->adminCacheService->removeAllCache();
        return ResponseUtil::success();
    }

}
